// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.population;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlTransient;
import zombie.core.ImmutableColor;
import zombie.util.StringUtils;
import zombie.util.list.PZArrayUtil;

public class ClothingItemReference implements Cloneable {
    public float probability = 1.0F;
    public String itemGUID;
    public ArrayList<ClothingItemReference> subItems = new ArrayList();
    public boolean bRandomized = false;
    @XmlTransient
    public boolean m_Immutable = false;
    @XmlTransient
    public final ClothingItemReference.RandomData RandomData = new ClothingItemReference.RandomData();

    public void setModID(String modID) {
        this.itemGUID = modID + "-" + this.itemGUID;

        for (ClothingItemReference clothingItemReference : this.subItems) {
            clothingItemReference.setModID(modID);
        }
    }

    public ClothingItem getClothingItem() {
        String string = this.itemGUID;
        if (!this.bRandomized) {
            throw new RuntimeException("not randomized yet");
        } else {
            if (this.RandomData.m_PickedItemRef != null) {
                string = this.RandomData.m_PickedItemRef.itemGUID;
            }

            return OutfitManager.instance.getClothingItem(string);
        }
    }

    public void randomize() {
        if (this.m_Immutable) {
            throw new RuntimeException("trying to randomize an immutable ClothingItemReference");
        } else {
            this.RandomData.reset();

            for (int _int = 0; _int < this.subItems.size(); _int++) {
                ClothingItemReference clothingItemReference1 = (ClothingItemReference)this.subItems.get(_int);
                clothingItemReference1.randomize();
            }

            this.RandomData.m_PickedItemRef = this.pickRandomItemInternal();
            this.bRandomized = true;
            ClothingItem clothingItem = this.getClothingItem();
            if (clothingItem == null) {
                this.RandomData.m_Active = false;
            } else {
                this.RandomData.m_Active = OutfitRNG.Next(0.0F, 1.0F) <= this.probability;
                if (clothingItem.m_AllowRandomHue) {
                    this.RandomData.m_Hue = (float)OutfitRNG.Next(200) / 100.0F - 1.0F;
                }

                if (clothingItem.m_AllowRandomTint) {
                    this.RandomData.m_Tint = OutfitRNG.randomImmutableColor();
                } else {
                    this.RandomData.m_Tint = ImmutableColor.white;
                }

                this.RandomData.m_BaseTexture = OutfitRNG.pickRandom(clothingItem.m_BaseTextures);
                this.RandomData.m_TextureChoice = OutfitRNG.pickRandom(clothingItem.textureChoices);
                if (!StringUtils.isNullOrWhitespace(clothingItem.m_DecalGroup)) {
                    this.RandomData.m_Decal = ClothingDecals.instance.getRandomDecal(clothingItem.m_DecalGroup);
                }
            }
        }
    }

    private ClothingItemReference pickRandomItemInternal() {
        if (this.subItems.isEmpty()) {
            return this;
        } else {
            int _int = OutfitRNG.Next(this.subItems.size() + 1);
            if (_int == 0) {
                return this;
            } else {
                ClothingItemReference clothingItemReference1 = (ClothingItemReference)this.subItems.get(_int - 1);
                return clothingItemReference1.RandomData.m_PickedItemRef;
            }
        }
    }

    public ClothingItemReference clone() {
        try {
            ClothingItemReference clothingItemReference0 = new ClothingItemReference();
            clothingItemReference0.probability = this.probability;
            clothingItemReference0.itemGUID = this.itemGUID;
            PZArrayUtil.copy(clothingItemReference0.subItems, this.subItems, ClothingItemReference::clone);
            return clothingItemReference0;
        } catch (CloneNotSupportedException cloneNotSupportedException) {
            throw new RuntimeException("ClothingItemReference clone failed.", cloneNotSupportedException);
        }
    }

    public static class RandomData {
        public boolean m_Active = true;
        public float m_Hue = 0.0F;
        public ImmutableColor m_Tint = ImmutableColor.white;
        public String m_BaseTexture;
        public ClothingItemReference m_PickedItemRef;
        public String m_TextureChoice;
        public String m_Decal;

        public void reset() {
            this.m_Active = true;
            this.m_Hue = 0.0F;
            this.m_Tint = ImmutableColor.white;
            this.m_BaseTexture = null;
            this.m_PickedItemRef = null;
            this.m_TextureChoice = null;
            this.m_Decal = null;
        }
    }
}
